namespace EFCoreManyToManyDemo;

public class Category
{
    public Category()
    {

    }

    public Product Product { get; set; }

    public string? Type { get; set; }
    public string? Description { get => Product.GetOrderItemDescription(); set => Product.SetOrderItemDescription(value); }
}

public class Product
{
    public Product()
    {
        OrderItems = new List<OrderItem>();
    }

    public Product(Order order) : this()
    {
        OrderItems.Add(new OrderItem()
        {
            Order = order,
            Product = this
        });
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public Category Category { get; set; }
    private ICollection<Order> Orders { get; set; }
    private ICollection<OrderItem> OrderItems { get; set; }
    public const string OrderName = nameof(Orders);
    public const string OrderItemName = nameof(OrderItems);

    private OrderItem OrderItem => OrderItems.First(o => o.ProductId == Id);

    public void SetCategory(string type, string description)
    {
        Category = new Category()
        {
            Product = this,
            Type = type,
            Description = description,
        };
    }

    public void ResetCategory(string type, string description)
    {
        Category = new Category()
        {
            Product = this,
            Type = type,
            Description = description,
        };
    }

    public string GetCategoryDescription()
    {
        return Category.Description!;
    }

    internal void SetOrderItemDescription(string description)
    {
        OrderItem.Description = description;
    }

    internal string GetOrderItemDescription()
    {
        return OrderItem.Description!;
    }

    internal int GetOrderItemId()
    {
        return OrderItem.Id;
    }
}